from django.shortcuts import render
from console.models import *
from django.shortcuts import render,redirect,HttpResponse
from django.db.models import Count
from django.http import JsonResponse
from django.http import HttpResponseForbidden, HttpResponseRedirect, JsonResponse, HttpResponse
# Create your views here.
def login(request):
    if request.method == 'GET':
        return render(request,'Adminage/login.html')
    else:
        user_name = request.POST.get('user_name')
        password = request.POST.get('password')
        print(user_name)
        try:
            user = User.objects.get(user_name=user_name,password=password)
            user_data = {
                'username':user.user_name,
                'id':user.id
            }
            request.session['user_data'] = user_data
            return redirect('index_data')
        except:
            return render(request, 'Adminage/login.html', {'msg': '*用户名密码输入有误'})

def register(request):
    if request.method == 'POST':
        name = request.POST.get('user_name')
        print('======================')
        print(name)
        email = request.POST.get('email')
        phone = request.POST.get('phone')
        password = request.POST.get('password')
        try:
            User.objects.create(user_name=name, password=password,phone=phone,nick_name=name,user_email=email)
        except:
            print('aaa')
        return render(request,'Adminage/login.html')
def index(request):
    fraud_month = fraud.objects.filter()
    return render(request,'Adminage/base.html')
def get_index(request):
    # fraud = AppFraudData.objects.filter(file_name=uid).values('fraud_type__name').annotate(count=Count('fraud_type_id'))
    month = fraud.objects.filter(year='2022').values('month').annotate(count=Count('month'))
    inter_data = fraud.objects.filter(month='5',type=1).values('day').annotate(count=Count('day'))
    phone_data = fraud.objects.filter(month='5',type=2).values('day').annotate(count=Count('day'))
    inter_year = fraud.objects.filter(year='2023',type=1).values('month').annotate(count=Count('month'))
    phone_year = fraud.objects.filter(year='2023',type=2).values('month').annotate(count=Count('month'))
    print(month)
    print(inter_data)
    print(phone_data)
    print(inter_year)
    print(phone_year)
    return render(request,'Adminage/index.html',{
        'month':month,
        'inter_data':inter_data,
        'phone_data':phone_data,
        'inter_year':inter_year,
        'phone_year':phone_year
    })
def get_list(request):
    if request.method == 'GET':
        url_data = Url_Data.objects.filter()
        for data in url_data:
            print(data.date_time)
        return render(request,'Adminage/data-table.html',{
            'url_data':url_data
        })
def get_list_phone(request):
    if request.method == 'GET':
        phone_data = phone_Data.objects.filter()
        return render(request,'Adminage/data-table-phone.html',{
            'phone_data':phone_data
        })
def get_profile(request):
    return render(request,'Adminage/profile.html')
def get_sticky(request):
    return render(request,'Adminage/sticky-note.html')
import json
import requests
import datetime
import socket
def detection(request):
    if request.method == 'POST':
        url_data = request.POST.get('url_data')
        type = request.POST.get('type')
        print(url_data)
        if type == '1':
            print('网址检测')
            try:
                data_url_false = data_False.objects.get(url=url_data)
                user_data = User.objects.get(id=request.session.get('user_data').get('id'))
                Url_Data.objects.create(url_name=data_url_false.url,url_user=user_data,url_type=data_url_false.url_type,
                                        url_content=data_url_false.url_content,url_ip=data_url_false.url_ip,
                                        url_address=data_url_false.url_address,date_time=datetime.datetime.now())
                data_text = {
                    'url': str(data_url_false.url),
                    'url_ip': str(data_url_false.ip),
                    'phone_type': str(data_url_false.get_url_type_display()),
                    'url_content': str(data_url_false.url_content),
                    'url_address': str(data_url_false.url_address)
                }
            except:
                user_data = User.objects.get(id=request.session.get('user_data').get('id'))
                ip_address = socket.gethostbyname(url_data)
                url = f'http://ip-api.com/json/{ip_address}'
                response = requests.get(url)
                address = response.json().get('timezone')
                data_url_false = Url_Data.objects.create(url_name=url_data, url_user=user_data,
                                        url_type=1,
                                        url_content='未检测到网址有违法行为', url_ip=ip_address,
                                        url_address=address, date_time=datetime.datetime.now())
                data_text = {
                    'url': str(data_url_false.url_name),
                    'url_ip': str(data_url_false.url_ip),
                    'phone_type': str(data_url_false.get_url_type_display()),
                    'url_content': str(data_url_false.url_content),
                    'url_address': str(data_url_false.url_address)
                }
        else:
            print('手机检测')
            try:
                phone = phone_False.objects.get(phone=url_data)
                user_data = User.objects.get(id=request.session.get('user_data').get('id'))
                phone_Data.objects.create(phone=phone.phone,phone_address=phone.phone_address,phone_user=user_data,
                                          phone_type=phone.phone_type,phon_content=phone.phone_content)
                data_text = {
                    'phone': str(phone.phone),
                    'phone_address': str(phone.phone_address),
                    'phone_type': str(phone.get_phone_type_display()),
                    'phone_content': str(phone.phone_content)
                }
            except:
                user_data = User.objects.get(id=request.session.get('user_data').get('id'))
                phone = phone_Data.objects.create(phone=url_data, phone_address='未检测到号码归属地', phone_user=user_data,
                                          phone_type=1, phon_content='号码暂无风险')
                data_text = {
                    'phone': str(phone.phone),
                    'phone_address': str(phone.phone_address),
                    'phone_type': str(phone.get_phone_type_display()),
                    'phone_content': str(phone.phon_content)
                }
        return JsonResponse(data_text, content_type='application/json; charset=utf-8')

def get_all_list(request):
    if request.method == 'GET':
        fraud_data = fraud.objects.filter()
        return render(request, 'Adminage/data-search.html',{'fraud_data':fraud_data})